/**
 * Created by chunzhao.li on 2016.12.26
 * 通知中心处理政务公开和网情舆情
 */
(function () {
    var egovern = function (config) {
        this._config = config;
        this._type = this.getConfig("type"); //公文类别
        this._businessType = this.getConfig("businessType");
        this._title = this.getConfig("title");
        this._businessId = this.getConfig("businessId");
        this._id = this.getConfig("id"); //公文id
        this._taskName = this.getConfig("businessType") == "0047"?"政务公开":"网情舆情";
        this._taskId = this.getConfig("taskId"); //待办id
        this._userId = this.getConfig("userId");
        this._div = (this).getConfig("div");
        this._createCardIds = this.getConfig("createCardIds");
        this.width = window.innerWidth || 320;
        this.height = window.innerHeight || 480;
        this.requestUrl = ""; //按钮操作交互地址
        this._format = d3.time.format("%Y-%m-%d");
    };
    if (typeof (Clickmed) === "undefined") {
        Clickmed = {};
    }
    Clickmed.egovern = egovern;
    egovern.prototype.init = function () {
        var me = this;
        window.plugins.getIntent.show(["OA", "正在获取数据"]);
        getDicts(function (dat) {
            me.Dicts = dat;
            var url = serverURL + "cmcp/user/getCurrentUserInfo";
            PostCP(url, null, function (dat) {
                me.userDat = dat;
                var url = serverURL + "cmcp/egovern/load";
                var self = {
                    id: me._businessId
                };
                console.log(self);
                //   向后台请求数据
                PostCP(url, self, function (dat) {
                    if (!dat) {
                        Prompt("获取数据失败");
                        return;
                    }
                    me.dat = dat;
                    me.draw();
                }, function () {
                    Alert("获取数据失败")
                })
            }, function () {
            });

        })

    };
    egovern.prototype.draw = function () {
        var me = this;
        var render = this._config.render || d3.select("body");
        var d1 = this._config.body || d3.select("body");
        var width = this.width;
        var scrol = d1.append("div").classed("scrol", true).style({
            width: this.width + "px",
            height: this.height + "px",
            position: "absolute",
            background: "#fff",
            left: "0px",
            "z-index": 99,
            top: "0px"
        }).attr("id", "scrol");
        var warp = scrol.append("div").classed("dt_warp", true).style({
            position: "relative"
        });
        me.showButton();
        var datong_header = warp.append("div").classed("datong_header", true);

        var go_back = datong_header.append("div").classed("go_back", true);
        go_back.html("<i><img src =../../common/page/images/go_back_ico.png width =20 height=auto></i>返回");
        go_back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        go_back.on(touch_e, function () {
            $(this).css("background-color", "");
            if (typeof (GLJ) != "undefined" && GLJ) {
                window.closeView();
                return;
            }
            setTimeout(function () {
                d1.selectAll("#scrol").remove();
                Android.back = TempFunction.back;
            }, Transiacion);

        });
        function back1() {

            setTimeout(function () {
                d1.selectAll("#scrol").remove();
            }, Transiacion);
            Android.back = TempFunction.back;
        }

        // Android返回键动作
        Android.back = TempFunction.back1 = back1;
        datong_header.append("h1").html(me._taskName || "");
        me.Redraw();

    };
    egovern.prototype.showButton = function () {
        var me = this;
        var warp = d3.select("#scrol .dt_warp");
        var button = warp.append("div").classed("tj_button2", true).classed("qb_btn_bg", true);
        var tj_button = button.append("p").style({
            "text-align": "center"
        });
        var input = tj_button.append("input").classed("other", true)
            .attr("type", "button")
            .attr("value", "阅 毕").style({
                width: "36.1%"
            });
        input.on("click", function () {
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            var url = serverURL + "cmcp/noticenter/setEndTodo"
            PostCP(url, me._id, function (dat) {
                if (!dat) {
                    Prompt("提交失败");
                    return
                }
                if (dat == "success") {
                    Prompt("提交成功");
                    me._div.remove();
                    iScrollDiv.refresh();
                    setTimeout(function () {
                        d3.selectAll("#scrol").remove();
                    }, Transiacion);
                    Android.back = TempFunction.back;
                }
            }, function () {
            });
        });

    }

    egovern.prototype.Redraw = function () {
        var me = this;
        var dat = me.dat;
        console.log("Redraw-通知信息详情:")
        console.log(me)
        var warp = d3.select("#scrol .dt_warp");
        var scrol_ = warp.append("div").classed("scrol_", true).style({
            width: "100%",
            height: height - 44 + "px",
            postion: "relative",
            overflow: "hidden"
        }).attr("id", "scrol_");
        var datong_con = scrol_.append("div").classed("datong_con datong_con_re", true);
        datong_con.append("div").classed("tongzhiTitle", true).style({
            "text-align": "center"
        }).html(dat["title"]);
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        var ul = datong_con.append("ul");
        var li = ul.append("li").classed("b_btm", true).style({
            width:"100%"
        });
        var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0").style({
            width:"100%"
        });
        var tr = table.append("tr");
        tr.append("td").html("<div>期号 </div>").style({
            width: "25%"
        });
        tr.append("td").style({
            width: "75%"
        }).append("div").html(dat["journalNum"]).classed("extend",true);
        var tr = table.append("tr");
        tr.append("td").html("<div>联系单位 </div>").style({
            width: "25%"
        });
        tr.append("td").style({
            width: "75%"
        }).append("div").html(dat["drafter"]).classed("extend",true);

        var tr = table.append("tr");
        tr.append("td").html("<div>联系电话 </div>").style({
            width: "25%"
        });
        tr.append("td").style({
            width: "75%"
        }).append("div").html(dat["drafterPhone"]).classed("extend",true);

        var tr = table.append("tr");
        tr.append("td").html("<div>市长批示 </div>").style({
            width: "25%"
        });
        tr.append("td").style({
            width: "75%"
        }).append("div").html(dat["sjOpinion"]).classed("extend",true);

        var tr = table.append("tr");
        tr.append("td").html("<div>副市长批示 </div>").style({
            width: "25%"
        });
        tr.append("td").style({
            width: "75%"
        }).append("div").html(dat["fsjOpinion"]).classed("extend",true);

        var tr = table.append("tr");
        tr.append("td").html("<div>秘书长批示 </div>").style({
            width: "25%"
        });
        tr.append("td").style({
            width: "75%"
        }).append("div").html(dat["mszOpinion"]).classed("extend",true);

        var tr = table.append("tr");
        tr.append("td").html("<div>副秘书长批示 </div>").style({
            width: "25%"
        });
        tr.append("td").style({
            width: "75%"
        }).append("div").html(dat["fmszOpinion"]).classed("extend",true);

        var tr = table.append("tr");
        tr.append("td").html("<div>转办单位回复 </div>").style({
            width: "25%"
        });
        tr.append("td").style({
            width: "75%"
        }).append("div").html(dat["departmentReply"]).classed("extend",true);
        var ps_con = datong_con.append("div").classed("ps_con", true);
        ps_con.html(dat["content"]);
        new iScroll("scrol_", {hScrollbar: false, vScrollbar: false});
        ps_con.selectAll("a").attr("href","#");


            // 正文列表
            var text = "/cmcp"+dat["text"];
            if(text && text!=''){
            var fileList = datong_con.append("div").classed("report_title", true).html("正文");
                
            var newDiv = datong_con.append("div").classed("newDiv", true)
                .attr("url", text).attr("fileName", "正文");
            ;
            var len = text.split(".").length;
            var extend = text.split(".")[len - 1];
            var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                width: "25",
                height: "22"

            }).style("margin-left", "15px");
            // 更改正文名字
            var gragh = newDiv.append("p").html("正文")
                .attr("url", text).attr("fileName", "正文");
            // 打开正文附件
            newDiv.on("click", function () {
                if (Flag)
                    return;
                Flag = true;
                setTimeout(function () {
                    Flag = false;
                }, 500);
                var url = $(this).attr("url");
                var fileName = $(this).attr("fileName");
                url = serverURL + url.substr(1);
                //   Alert(url);
                window.openF(url, fileName);
            });
            }
           
            // 附件列表
            var attachmentInfo = dat["files"];
            if (attachmentInfo && attachmentInfo.length) {
                var fileList = datong_con.append("div").classed("report_title", true).html("附件");
                for (var i = 0; i < attachmentInfo.length; i++) {
                    var newDiv = datong_con.append("div").classed("newDiv", true)
                        .attr("url", attachmentInfo[i]["pdfUrl"]).attr("fileName", attachmentInfo[i]["name"]);
                    ;
                    var len = attachmentInfo[i]["pdfUrl"].split(".").length;
                    var extend = attachmentInfo[i]["pdfUrl"].split(".")[len - 1];
                    var img = newDiv.append("img").attr("src", "../../common/page/images/" + extend + ".png").attr({
                        width: "25",
                        height: "22"

                    }).style("margin-left", "15px");
                    var file = newDiv.append("p").html(attachmentInfo[i]["name"])
                        .attr("url", attachmentInfo[i]["pdfUrl"]).attr("fileName", attachmentInfo[i]["name"]);
                    // 打开附件
                    newDiv.on("click", function () {
                        var url = $(this).attr("url");
                        if (Flag)
                            return;
                        Flag = true;
                        setTimeout(function () {
                            Flag = false;
                        }, 500);
                        url = serverURL + url.substr(1);
                        var fileName = $(this).attr("fileName");

                        window.openF(url, fileName);

                    });
                }
            }


        window.plugins.getIntent.hide();
    };
 
    egovern.prototype.getConfig = function (key) {
        if (this._config == null) {
            return null;
        } else {
            if (typeof (this._config[key]) != "undefined") {
                return this._config[key];
            } else {
                return null;
            }
        }
    };
})()